/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucene.simple.search;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 *
 * @author Content Pilot
 * Create Indexes for Dir
 */
public class SimpleFileIndexer {
    
    static Analyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_34);
    static Analyzer simpleAnalyzer = new SimpleAnalyzer(Version.LUCENE_34);
    
    
    public static void main(String[] args) throws Exception { 
        File indexDir = new File("c:/luence_index/");
        File dataDir = new File("D:/Documents and Settings/Content Pilot/My Documents/WORK/SITES");
        String suffix = "vhtml";

        SimpleFileIndexer indexer = new SimpleFileIndexer();
        int numIndex = indexer.createIndex(indexDir, dataDir, suffix);
        
        System.out.println("Total files indexed " + numIndex);
    
    }
    
    //create index using dataDir
    private int createIndex(File indexDir, File dataDir, String suffix) throws Exception { 
        Directory dir = FSDirectory.open(indexDir);
        
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_34, standardAnalyzer); // new in 3.4 use IndexWriter(Directory, IndexWriterConfig) instead
        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); // Open Mode default value
        
        IndexWriter indexWriter = new IndexWriter(dir,conf);
        indexDirectory(indexWriter, dataDir, suffix);
        
        int numIndexed = indexWriter.maxDoc();
        indexWriter.optimize();
        indexWriter.close();
        
        return numIndexed;
        
    }
    
    // scan data dir
    private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
    
        File[] files = dataDir.listFiles();
        for ( int i = 0; i < files.length; i++ ){
            File f = files[i];
            if( f.isDirectory()){
                indexDirectory( indexWriter, f, suffix );
            } else {
                indexFileWithIndexWriter(indexWriter, f, suffix);
            }
        }
    }
    
    // create index file
    private void indexFileWithIndexWriter(IndexWriter indexWriter, File file, String suffix) throws IOException {
        if( file.isHidden() || file.isDirectory() || !file.canRead() || !file.exists() ) {
            return;
        }
        if( (suffix != null || suffix !="") && !file.getName().endsWith(suffix) ) {
            return;
        }
        System.out.println("Indexing file " + file.getCanonicalPath());
        
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(file)));
        doc.add(new Field("filename", file.getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
        
        indexWriter.addDocument(doc);

    }
}
